<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:management:export']"
        >导出</el-button>
      </el-col> -->
      <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="状态" align="center" prop="id" /> -->
      <el-table-column label="名称" align="center" prop="name" />
      <!-- <el-table-column label="文件路径" align="center" prop="fileUrl" /> -->
      <el-table-column label="工程款支付担保制度" align="center" prop="fileName" />
      <el-table-column label="总包单位农民工工资专用账户制度" align="center" prop="fileTwoName" />
      <el-table-column label="总包代付工资制度，实名制管理制度" align="center" prop="fileThreeName" />
      <el-table-column label="分账管理制度" align="center" prop="fileFourName" />
      <el-table-column label="分账管理制度" align="center" prop="fileFiveName" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="批注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="operationDetail(scope.row)"
          >审核记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.status === '审核失败'"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="agree(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >同意</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="reject(scope.row)"
            v-if="roleKey === 'common'&&scope.row.status === '待审核'"
          >驳回</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-finished"
            @click="submitAgain(scope.row)"
            v-if="scope.row.status === '审核失败'" 
          >重新提交</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改五项制度对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="工程款支付担保制度" prop="field101" >
        <el-upload ref="field101" :file-list="field101fileList" :action="field101Action"
        :on-preview="handlePreview"
        :on-success="handleSuccess"
          >
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="分账管理制度" prop="field105" >
        <el-upload ref="field105" :file-list="field105fileList" :action="field101Action"
        :on-preview="handlePreview"
        :on-success="handleSuccess5"
          >
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="总包单位农民工工资专用账户制度" prop="field102" >
        <el-upload ref="field102" :file-list="field102fileList" :action="field101Action"
        :on-preview="handlePreview"
        :on-success="handleSuccess2"
          >
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="总包代付工资制度，实名制管理制度" prop="field103" >
        <el-upload ref="field103" :file-list="field103fileList" :action="field101Action"
        :on-preview="handlePreview"
        :on-success="handleSuccess3"
          >
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="分账管理制度" prop="field104" >
        <el-upload ref="field104" :file-list="field104fileList" :action="field101Action"
        :on-preview="handlePreview"
        :on-success="handleSuccess4"
          >
          <el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
        </el-upload>
      </el-form-item>
        <el-form-item label="批注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入批注" readonly />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审批意见" :visible.sync="open1" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules1" label-width="80px">
        <el-form-item label="备注" >
          <el-input v-model="form.remark" type="textarea" placeholder="请输入多行文本"
          :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}" ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejectSub">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核记录" :visible.sync="open2">
  <el-table :data="operationList">
    <el-table-column property="content" label="内容" width="150"></el-table-column>
    <el-table-column property="userName" label="操作人" width="200"></el-table-column>
    <el-table-column property="time" label="操作时间">
      <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
    </el-table-column>
  </el-table>
</el-dialog>
  </div>
</template>

<script>
import { listManagement, getManagement, delManagement, addManagement, updateManagement, exportManagement,agree,submitAgain,reject,operations } from "@/api/system/management";

export default {
  name: "Management",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 五项制度表格数据
      managementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open1:false,
      open2:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        fileUrl: null,
        fileName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      field101: null,
      field102: null,
      field103: null,
      field104: null,
      field105: null,
      field101Action: process.env.VUE_APP_BASE_API + "/common/upload",
      field101fileList: [],
      field102fileList: [],
      field103fileList: [],
      field104fileList: [],
      field105fileList: [],
      options: [{
          value: '待审核',
          label: '待审核'
        }, {
          value: '审核成功',
          label: '审核成功'
        }, {
          value: '审核失败',
          label: '审核失败'
        }],
        rules1: {
        remark: [
          { required: true, trigger: "blur", message: "请输入审批意见" }
        ]
      },
      username:"",
      operationList:[],
      roleKey:""
    };
  },
  created() {
    this.getList();
    this.username = this.$store.state.user.name;
  },
  methods: {
    reject(row){
      this.form.id = row.id;
      this.open1=true;
    },
    cancel1(){
      this.open1=false;
    },
    agree(row){
      this.form.id = row.id;
      agree(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.getList();
            });
    },
    rejectSub(){
      reject(this.form).then(response => {
              this.$modal.msgSuccess("审批成功");
              this.open1 = false;
              this.getList();
            });
    },
    submitAgain(row){
      this.form.id = row.id;
      submitAgain(this.form).then(response => {
              this.$modal.msgSuccess("重新发起成功");
              this.getList();
            });
    },
     handleSuccess(res, file, fileList){
        let name = "";
        let url = "";
        for(let i=0;i<fileList.length;i++){
          if(fileList[i].response===undefined){
            url += fileList[i].url+",";
            name += fileList[i].name+",";
          }else{
            url += fileList[i].response.fileName+",";
            name += fileList[i].name+",";
          }
          
        }
        this.form.fileUrl = url;
        this.form.fileName = name;
    }, 
    handleSuccess2(res, file, fileList){
        let name = "";
        let url = "";
        for(let i=0;i<fileList.length;i++){
          if(fileList[i].response===undefined){
            url += fileList[i].url+",";
            name += fileList[i].name+",";
          }else{
            url += fileList[i].response.fileName+",";
            name += fileList[i].name+",";
          }
          
        }
        this.form.fileTwoUrl = url;
        this.form.fileTwoName = name;
    }, 
    handleSuccess3(res, file, fileList){
        let name = "";
        let url = "";
        for(let i=0;i<fileList.length;i++){
          if(fileList[i].response===undefined){
            url += fileList[i].url+",";
            name += fileList[i].name+",";
          }else{
            url += fileList[i].response.fileName+",";
            name += fileList[i].name+",";
          }
          
        }
        this.form.fileThreeUrl = url;
        this.form.fileThreeName = name;
    }, 
    handleSuccess4(res, file, fileList){
        let name = "";
        let url = "";
        for(let i=0;i<fileList.length;i++){
          if(fileList[i].response===undefined){
            url += fileList[i].url+",";
            name += fileList[i].name+",";
          }else{
            url += fileList[i].response.fileName+",";
            name += fileList[i].name+",";
          }
          
        }
        this.form.fileFourUrl = url;
        this.form.fileFourName = name;
    }, 
    handleSuccess5(res, file, fileList){
        let name = "";
        let url = "";
        for(let i=0;i<fileList.length;i++){
          if(fileList[i].response===undefined){
            url += fileList[i].url+",";
            name += fileList[i].name+",";
          }else{
            url += fileList[i].response.fileName+",";
            name += fileList[i].name+",";
          }
          
        }
        this.form.fileFiveUrl = url;
        this.form.fileFiveName = name;
    }, 
    handlePreview(file) {
      let url = "";
      if(file.url===undefined){
        url = file.response.url;
      }else{
        url = file.url;
      }
      this.$download.resource(url)
    },
    /** 查询五项制度列表 */
    getList() {
      this.field101fileList=[]
      this.field102fileList=[]
      this.field103fileList=[]
      this.field104fileList=[]
      this.field105fileList=[]
      this.loading = true;
      listManagement(this.queryParams).then(response => {
        this.managementList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.roleKey = response.rows[0].roleKey
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.field101fileList=[]
      this.field102fileList=[]
      this.field103fileList=[]
      this.field104fileList=[]
      this.field105fileList=[]
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        fileUrl: null,
        fileName: null,
        status: "0",
        remark: null
      };
      this.field101fileList=[]
      this.field102fileList=[]
      this.field103fileList=[]
      this.field104fileList=[]
      this.field105fileList=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加五项制度";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManagement(id).then(response => {
        this.form = response.data;
        response.data.fileUrlList.forEach(item => {  //处理图片需要转格式
        this.field101fileList.push({
          "url": item.url,
          "name":item.name
        })
        this.field102fileList.push({
          "url2": item.url,
          "name2":item.name
        })
        this.field103fileList.push({
          "url3": item.url,
          "name3":item.name
        })
        this.field104fileList.push({
          "url4": item.url,
          "name4":item.name
        })
        this.field105fileList.push({
          "url5": item.url,
          "name5":item.name
        })
      })
        this.open = true;
        this.title = "修改五项制度";
      });
    },
    operationDetail(row){
      const id = row.id
      operations(id).then(response => {
        this.operationList = response.data;
        this.open2 = true;
      });
      
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除五项制度编号为"' + ids + '"的数据项？').then(function() {
        return delManagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有五项制度数据项？').then(() => {
        this.exportLoading = true;
        return exportManagement(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
